﻿// CWndAddFunc.cpp: 实现文件
//

#include "pch.h"
#include "GameHacker.h"
#include "CWndAddFunc.h"
#include "afxdialogex.h"
#include "CMemContext.h"
extern DATA_TYPE data_type[12];


CString arrys[]
{
L"void",
L"bool",
L"char",
L"unsigned char",
L"short",
L"unsigned short",
L"int",
L"unsigned",
L"long long",
L"unsigned long long",
L"float",
L"double",
L"void*"
};

// CWndAddFunc 对话框

IMPLEMENT_DYNAMIC(CWndAddFunc, CDialogEx)

CWndAddFunc::CWndAddFunc(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_ADDFUNC, pParent)
	, sType(_T(""))
	, sName(_T(""))
	, sAddress(_T(""))
	, sNote(_T(""))
{

}

CWndAddFunc::~CWndAddFunc()
{
}

void CWndAddFunc::Init(PCFuncContext val, PCObjContext obj)
{
	if(val){//导入到对话框
		pParamContext = val->Params;
	}
	_ObjContext = obj;
}

void CWndAddFunc::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_lstType);
	DDX_Text(pDX, IDC_EDIT2, sType);
	DDX_Text(pDX, IDC_EDIT1, sName);
	DDX_Text(pDX, IDC_EDIT10, sAddress);
	DDX_Text(pDX, IDC_EDIT11, sNote);
	DDX_Control(pDX, IDC_LIST1, m_lstParam);
}


BEGIN_MESSAGE_MAP(CWndAddFunc, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &CWndAddFunc::OnBnClickedButton1)
	ON_BN_CLICKED(IDOK, &CWndAddFunc::OnBnClickedOk)
END_MESSAGE_MAP()


// CWndAddFunc 消息处理程序


void CWndAddFunc::OnBnClickedButton1()
{
	wndAddParam.Init(pParamContext);
	if (wndAddParam.DoModal() == IDOK) {
		if (!pParamContext)
			pParamContext = new CParamContext(wndAddParam.sName, wndAddParam.sType, wndAddParam.sNote, nullptr);
		else
			pParamContext->Add(wndAddParam.sName, wndAddParam.sType, wndAddParam.sNote);

		CString val;
		val.Format(L"%s %s", wndAddParam.sType.GetBuffer(),
			wndAddParam.sName.GetBuffer());
		m_lstParam.AddString(val);
	}
}


BOOL CWndAddFunc::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	for (auto& x : data_type) {
		m_lstType.AddString(x.sName);
	}
	m_lstType.SetCurSel(nTypeId);

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CWndAddFunc::OnBnClickedOk()
{
	UpdateData(TRUE);
	int nTypeId = m_lstType.GetCurSel();
	if (!_ObjContext->TestContextName(sName)) {
		AfxMessageBox(L"函数名与变量名重复");
		return;
	}

	auto p = pParamContext;
	CString val, valParams;
	while (p) {
		valParams = valParams + p->sType + L",";
		p = p->Next;
	}
	val.Format(L"%s(%s)", sName, valParams);

	if (!_ObjContext->TestFuncName(val)) {
		AfxMessageBox(L"函数名与其他函数重复");
		return;
	}

	if (sType == L"") {
		sType = arrys[m_lstType.GetCurSel()];
	}

	pFuncReturn = _ObjContext->AddFuncContext(sName, sType, sNote, L"", 0, pParamContext);

	CDialogEx::OnOK();
}
